public  uart_init
public  int0_inter
public  serial_send



ss2      segment     code
rseg    ss2



rev_counter     equ     43h
counter         equ     44h
output          equ     45h
rev_buffer      equ     46h
newRXD          bit     p3.2
rev_start       bit     20h.3         ;0
rev_stop        bit     20h.4         ;1


;--------------------------------------------------------------
uart_init:
  

        mov     scon,#40h   ;it was 50h, we need clr ren, so we
                            ; will not be influenced by receiving
                            ;data from GSM module
        mov     tmod,#22h
        mov     tl1,#0fdh
        mov     th1,#0fdh
    
        mov     ie,#91h
        clr     tf0
        setb    it0
        setb    tr1
        ret  
;--------------------------------------------------------------


;--------------------------------------------------------------
int0_inter:
        clr     ex0
        push    0
        push    acc
        push    dpl
        push    dph
        ;mov     r0,rev_buffer
    
        mov     r0,rev_buffer  ;;;****
    
        call    read_byte
        mov     a,output
        
        cjne    a,#24h, check0_int0_inter       ;24h='$'
        setb    rev_start
        clr     rev_stop

check0_int0_inter:
        jnb      rev_start,exit_int0_inter   
        ;mov     a,output
        mov     @r0,a
        inc     rev_buffer  ;;*** inc r0
        
        cjne    a,#0ah,exit_int0_inter
        clr     rev_start
        setb    rev_stop
        mov     rev_buffer,#50h  ;;;;;;;;;;; reverse value of rev_buffer
        ;;reti    ;;;;;;;
    
    



exit_int0_inter:
        clr     ie0
        pop     dph
        pop     dpl
        pop     acc
        pop     0
        setb    ex0
        ret
;--------------------------------------------------------------


;--------------------------------------------------------------
read_byte:
        mov     output,#00h
        mov     counter,#08h
        setb    tr0
        mov     th0,#0a0h
        mov     tl0,#0a0h
        clr     tf0
        call    wait_tf0

next0_read_byte:
        mov     a,output
        clr     c
        rrc     a
        ;rr      a  ;;;;
        mov     output,a
    
        jnb     newRXD,next1_read_byte
        mov     a,output
        orl     a,#80h
        mov     output,a    ;;;;;;
   

next1_read_byte:
    
        call    wait_tf0
        djnz    counter,next0_read_byte
    
       ; call    a_test_bit  
        
      ;  jnc     read_byte
       
    
        clr     tr0
        ret
;--------------------------------------------------------------


;-----------------------------------------------------
wait_tf0:
        jnb     tf0,$
        clr     tf0
        ret
;-----------------------------------------------------



;-----------------------------------------------------
;Name of Subprogram: serial
;Function:subprogram for supporting function send
;input:a
;output:None
;Effects: sbuf,ti
;-----------------------------------------------------

serial_send:
    clr     es
    mov     sbuf,a
    jnb     ti,$
    clr     ti
    setb    es
    ret
;-----------------------------------------------------


end
